﻿@model LeadMaster.ViewModels.ViewModelContacts

<div class="clearer"></div>
<img src="~/Images/user_add.png"    class="addcontact navbuttons"   id="useradd@(@Model.Company.CompanyID)" />
<div>
<table class="listtable contactstable" cellspacing="0" cellpadding="0">
<thead>
    <tr>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Forename)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Initials)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Surname)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Title)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.CompanyID)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Address1)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Town)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.County)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Postcode)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Telephone)
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Email)
        </th>
        <th class="tableheader"></th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model.Contacts) {
    if (item.ContactID>1)
    {
    <tr id="row@(item.ContactID)">
        <td>
            @Html.ActionLink(item.Forename, "Edit","Contacts", new { id=item.ContactID  })
        </td>
        <td>
        @if (item.Initials!=null && item.Initials!=string.Empty)
        {
            @Html.ActionLink(item.Initials, "Edit","Contacts", new { id = item.ContactID })
        }
        </td>
        <td>
            @Html.ActionLink(item.Surname, "Edit", "Contacts", new { id = item.ContactID })
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Company.Company)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Town)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.County)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Postcode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Telephone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            <img src="~/Images/cross.png"    class="deletecontact navbuttons"   id="delete@(item.ContactID)" />
        </td>
    </tr>
    }
}
</tbody>
</table>
</div>

<div id="addcontactdialog" class="overlay">
</div>


<script type="text/javascript">
    $(document).ready(function () {

        $(".deletecontact").click(function () {
            var contactID = $(this).attr('id').substring(6);
            alert("id=" + contactID.toString());
            var urlString='@Url.Action("Delete","Contacts" , new { id = "_contactID_" })';
            urlString=urlString.replace('_contactID_',contactID);
            $.ajax({
                type: "POST",
                url: urlString,
                success: function (data) {
                    $('#row' + id).fadeOut(500, function () { $('#row' + data).remove(); });
                }
            });


        });

        $(".addcontact").click(function () {
            var companyID = $(this).attr('id').substring(7);
            $("#addcontactdialog").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            $('#addcontactdialog').load('@(Url.Action("CreateWithCompany", "Contacts"))/' + companyID,
            function () {
                $("#addcontactdialog").dialog("close");
                $("#addcontactdialog").dialog(
                {
                    autoOpen: true,
                    width: 1000,
                    open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
                    position: { my: 'center', at: 'center', of: window },
                    modal: true,
                    resizable: false,
                    draggable: false,
                    dialogClass: 'notitle'
                });
            });
            $("#addcontactdialog").dialog(
                {
                    autoOpen: true,
                    width: 200,
                    position: { my: 'center', at: 'center', of: window },
                    modal: true,
                    resizable: false,
                    draggable: false,
                    dialogClass: 'notitle'
                });
        });


    });
</script>